﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Notes_UserControls_NTS_SideSearch : System.Web.UI.UserControl
{

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            BindStream();
        }
    }


    #region Search

    Notes.NotesDB _objNotesDB = new Notes.NotesDB();

    protected void BindStream()
    {
        IDataReader dr;
        ddlStream.Items.Clear();
        ListItem lst1 = new ListItem();
        lst1.Text = "Select";
        lst1.Value = "0";
        ddlStream.Items.Add(lst1);
        dr = _objNotesDB.Select_Streams();
        if (dr != null)
        {
            while (dr.Read())
            {
                ListItem lst = new ListItem();
                lst.Text = dr["Stream"].ToString();
                lst.Value = dr["StreamID"].ToString();
                ddlStream.Items.Add(lst);
            }
        }
        dr.Close();
    }


    protected void BindSubject()
    {
        int StreamID = 0;
        IDataReader dr;
        ddlSubject.Items.Clear();
        ListItem lst1 = new ListItem();
        lst1.Text = "Select";
        lst1.Value = "0";
        ddlSubject.Items.Add(lst1);
        if (ddlStream.SelectedItem.Text != "Select")
        {
            StreamID = Convert.ToInt32(ddlStream.SelectedValue.ToString());
        }
        else
        {
            StreamID = 0;
        }
        dr = _objNotesDB.Select_Subjects(StreamID);
        if (dr != null)
        {
            while (dr.Read())
            {
                ListItem lst = new ListItem();
                lst.Text = dr["SubjectName"].ToString();
                lst.Value = dr["SubjectID"].ToString();
                ddlSubject.Items.Add(lst);
            }
        }
        dr.Close();
    }


    protected void ddlStream_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindSubject();
    }


    bool Validate()
    {
        if (txtName.Text.Trim() == string.Empty && ddlStream.SelectedItem.Text == "Select" && ddlSubject.SelectedItem == null && ddlDocType.SelectedItem.Text == "Select")
        {
            return false;
        }
        return true;
    }

    protected void btnSearch_Click(object sender, ImageClickEventArgs e)
    {
        if (Validate())
        {
            Session["DocName"] = null;
            Session["StreamID"] = null;
            Session["SubjectID"] = null;
            Session["DocTypeID"] = null;
            if (txtName.Text != string.Empty)
            {
                Session["DocName"] = txtName.Text;
            }
            if (ddlStream.SelectedItem != null)
            {
                if (ddlStream.SelectedItem.Text != "Select")
                {
                    Session["StreamID"] = ddlStream.SelectedValue;
                    Session["StreamName"] = ddlStream.SelectedItem.Text;
                }
                if (ddlSubject.SelectedItem != null)
                {
                    if (ddlSubject.SelectedItem.Text != "Select")
                    {
                        Session["SubjectID"] = Convert.ToInt32(ddlSubject.SelectedValue.ToString());
                        Session["SubjectName"] = ddlSubject.SelectedItem.Text;
                    }
                }
            }
            if (ddlDocType.SelectedItem.Text != "Select")
            {
                Session["DocTypeID"] = ddlDocType.SelectedValue;
                Session["DocTypeName"] = ddlDocType.SelectedItem.Text;
            }
            Response.Redirect("NTS-DocSearch.aspx");
        }
        else
        {
            //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "mykey", "giveAlert();",true);

        }
    }
    #endregion
}